package demo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {
//        Stream<Integer> randoms = Stream.generate(() -> (int)(Math.random() * 100)).limit(100);
//        randoms.forEach(System.out::println);

//        Stream<String> combined = Stream.concat(letters("Hello"), letters("world")).skip(2);
//        combined.forEach(System.out::print);

//        Stream<String> uniqueWords = Stream.of("hello", "hello", "hello", "word").distinct();
//        uniqueWords.forEach(System.out::println);

//        Stream<String> longestFirst =
//                Stream.of("one", "two", "three", "four", "five")
//                .sorted(Comparator.comparing(String::length).reversed());
//        longestFirst.forEach(System.out::println);

//        Object[] powers = Stream.iterate(1.0, p -> 2 * p)
//                .peek(e -> System.out.println("Fetching " + e))
//                .limit(20).toArray();
//        System.out.println(powers);

//        Stream<String> words = Stream.of("one", "two", "three", "four", "five", "six");
//        Optional<String> largest = words.max(String::compareToIgnoreCase);
//        System.out.println("largest: " + largest.orElse(""));

//        Stream<String> words = Stream.of("one", "two", "three", "four", "five", "six", "seven", "Qone", "Qtwo");
//        Optional<String> startsWithQ = words.filter(str -> str.startsWith("Q")).findFirst();
//        System.out.println("First string starts with \"Q\": " + startsWithQ.orElse("null"));
//        words = Stream.of("one", "two", "three", "four", "five", "six", "seven", "Qone", "Qtwo");
//        Optional<String> parallelStartsWithQ = words.parallel()
//                .filter(str -> str.startsWith("Q")).findAny();
//        System.out.println("Starts with\"Q\": " + parallelStartsWithQ.orElse("null"));
//        words = Stream.of("one", "two", "three", "four", "five", "six", "seven", "Qone", "Qtwo");
//        boolean aWordStartsWithQ = words.parallel().anyMatch(str -> str.startsWith("Q"));
//        System.out.println("Has a word starts with \"Q\": " + aWordStartsWithQ);

        Stream<String> words = Stream.of("one", "two", "three", "four", "five", "six", "seven", "Qone", "Qtwo");
        Optional<String> parallelStartsWithX = words.parallel()
                .filter(str -> str.startsWith("X")).findAny();
        System.out.println("Starts with\"X\": " + parallelStartsWithX.orElseGet(
                () -> Locale.getDefault().getDisplayName()));
    }

    public static Stream<String> letters(String str) {
        ArrayList<String> result = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
            result.add(str.substring(i, i+1));
        }
        return result.stream();
    }
}
